package com.wanlian.service.impl;

import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.common.UrlConfig;
import com.wanlian.context.Notice.NoticeMsgContext;
import com.wanlian.dao.NoticeMsgDao;
import com.wanlian.dao.UserDao;
import com.wanlian.entity.NoticeMsg;
import com.wanlian.entity.User;
import com.wanlian.error.SuccessCode;
import com.wanlian.jpush.service.PushService;
import com.wanlian.model.ResultModel;
import com.wanlian.service.NoticeMsgServices;
import com.wanlian.util.DateUtil;
import com.wanlian.vo.NoticeMsgResultVo;
import com.wanlian.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.List;
import java.util.Map;

@Component
public class NoticeMsgServicesImpl implements NoticeMsgServices {
    @Autowired
    private NoticeMsgDao noticeMsgDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private PushService pushService;

    @Autowired
    private UrlConfig  urlConfig;

    @Override
    public ResultModel doDeleteMsg(NoticeMsg msg) {
        ResultModel resultModel = new ResultModel();
        noticeMsgDao.delete(msg);
        resultModel.setBresult(true);
        resultModel.setMessage(SuccessCode.STORAGE_DELETE);
        return resultModel;
    }

    @Override
    public ResultModel doUpdateMsg(NoticeMsg msg) {

        ResultModel resultModel = new ResultModel();
        noticeMsgDao.update(msg);
        resultModel.setBresult(true);
        resultModel.setMessage(SuccessCode.SUCCESS_UPD);
        return resultModel;
    }

    @Override
    public ResultModel queryNoticeMsgById(int id) {
        NoticeMsg noticeMsg = noticeMsgDao.queryById(id);
        ResultModel resultModel = new ResultModel();
        resultModel.setBresult(true);
        resultModel.setObject(noticeMsg);
        return resultModel;
    }

    @Override
    public ResultModel queryNoticeMsgs(PageCondition pageCondition) {
        ResultModel resultModel = noticeMsgDao.queryNoticeMsgs(pageCondition);
        resultModel.setBresult(true);
        return resultModel;
    }

    @Override
    public ResultModel doSendMsgForOne(NoticeMsg noticeMsg) {
        ResultModel resultModel = noticeMsgDao.sendMsgForOne(noticeMsg);
        return resultModel;
    }

    @Override
    public ResultModel doSendMsgForMany(NoticeMsg noticeMsg) {

        ResultModel resultModel = noticeMsgDao.sendMsgForMany(noticeMsg);
        return resultModel;
    }

    @Override
    public ResultModel queryCountByUserId(int userid) {
        ResultModel resultModel = noticeMsgDao.queryCountByUserId(userid);
        return resultModel;
    }

    @Override
    public ResultModel queryMsgByUserId(int userId, Page page) {
        return noticeMsgDao.queryMsgByUserId(userId, page);
    }

    @Override
    public ResultModel doSendSystemmsg(NoticeMsg noticeMsg) {
        ResultModel resultModel = new ResultModel();
        List<User> users = userDao.queryAll();

        String timestap = DateUtil.currentTimestamp().getTime() + "";
        String url =  String.format("%s%s.html",urlConfig.getNoticeFilePath(),timestap );

        NoticeMsgServicesImpl.CreateFiles(noticeMsg.getContent(),url);
        String getUrl = String.format("%s%s.html",urlConfig.getNoticeDirPath(),timestap );
        //发消息
        for (User user : users) {
            //pushService.sendMessageBySingleTag(user.getPhone(),noticeMsg.getTitle(),noticeMsg.getContent());
            NoticeMsg noticeMsg1 = new NoticeMsg();
            noticeMsg1.setTitle(noticeMsg.getTitle());
            noticeMsg1.setContent(noticeMsg.getContent());
            noticeMsg1.setUrl(getUrl);
            noticeMsg1.setAdmin(noticeMsg.getAdmin());
            noticeMsg1.setSendName(user.getUsername());
            noticeMsg1.setUserId(user.getId());
            noticeMsg1.setNoticeType(0);
            noticeMsg1.setSourceType(ConstantCode.NOTICE_SYSTEM);
            noticeMsgDao.add(noticeMsg1);

            NoticeMsgResultVo notice = new NoticeMsgResultVo();
            notice.setNoticeMsg(noticeMsg1);
            notice.setUser(user);
            NoticeMsgContext.add(notice);
        }
        resultModel.setBresult(true);
        resultModel.setMessage(SuccessCode.NOTICE_SEND);
        return resultModel;
    }


    /**
     * 创建一个文件>>>>解决中文乱码
     * @param content
     * @param path
     * @return
     */
    public static String CreateFiles(String content,String path) {
        File file = null;
        try {
            file = new File(path);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            bw.write(content);
            bw.flush();
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }



    @Override
    public ResultModel doUpdateHasLook(int noticeId) {
        ResultModel resultModel = noticeMsgDao.updateHasLook(noticeId);
        return resultModel;
    }

    @Override
    public ResultModel querySystem(int userId, Page page) {
        return noticeMsgDao.querySystem(userId, page);
    }

    @Override
    public ResultModel queryCountSystem(int userId) {
        return noticeMsgDao.queryCountSystem(userId);
    }

    @Override
    public ResultModel queryAllSystem(PageCondition pageCondition) {
        return noticeMsgDao.queryAllSystem(pageCondition);
    }

    @Override
    public List<NoticeMsg> queryAllByUserId(int userId) {
        return noticeMsgDao.queryAllByUserId(userId);
    }

    @Override
    public NoticeMsg queryNewSystemByUserId(int userId) {
        return noticeMsgDao.queryNewSystemByUserId(userId);
    }


}
